/**
 * 无限级别分类(SELECT标签版)
 * @param data 树形分类数据
 * @param cont 容器ID
 * @param defaultOption 默认option
 * @date 2018 12 28
 * @author YUE
 */
function NoLimitCategory(data, cont, defaultOption){
	if(typeof(arguments[0]) != 'object'){alert("无限极分类：缺少数据参数");return};
	if(typeof(arguments[1]) != 'object'){alert("无限极分类：缺少容器对象");return};
	if(!arguments[2])defaultOption = "";
	
	if(!(data.length > 0)){
		alert("无限极分类：数据不完整");return;
	}
	
	var self = this;
	
	this.onSelectCreate = function(selectObj){};
	
	this.append = function(){
		var isContinue = defaultOption ? false : true;
		appendSelect(data, isContinue);	
	}

    /**
	 * 递归追加SELECT标签
     * @param data 数据
     * @param isContinue 是否追加下一级
     */
	var appendSelect = function(data, isContinue){

		var select = document.createElement("select");

		cont.appendChild(select);
        self.onSelectCreate(select);

		if(data.length > 0 && typeof(defaultOption) == 'string' && defaultOption != ''){
			var option = document.createElement("option");
			option.value = 0;
			option.innerHTML = defaultOption;
			select.appendChild(option);
		}
		
		for(var i=0; i<data.length; i++){

			var option = document.createElement("option");
			option.value = data[i].id;
			option.innerHTML = data[i].name;
			select.appendChild(option);
			
			if(i == 0){
				var childrenData = (typeof(data[0].children) == 'undefined') ? null : data[0].children;
			}

		}

		if(isContinue && childrenData != null && childrenData.length > 0){
			appendSelect(childrenData);
		}
		
		select.onchange = function(){
			
			removeSelectAfter(select);

			var childrenData = [];
			
			for(var i=0; i<data.length; i++){
				if(data[i].id == select.value){
					childrenData = (typeof(data[i].children) == 'undefined') ? null : data[i].children;
				}
			}

			if(childrenData != null && childrenData.length > 0){
				appendSelect(childrenData);
			}

		}

	}
	
	//删除选择的SELECT后的所有SELSECT
	var removeSelectAfter = function(seleObj){
		
		var selects = cont.getElementsByTagName("select");
		var length = selects.length;
		var temp = 0;
		
		//先找出选择的是第几个SELECT
		for(var i=0; i<length; i++){
			if(selects[i] == seleObj){	
				temp = i;
			}	
		}

		//删除当前SELECT后的所有SELECT
		for(var i=0; i<(length - temp -1); i++){
			cont.removeChild(selects[temp+1]);
		}

	};

    /**
     * 输出已选的分类ID字符串，用逗号隔开，例 1,5,28 依次为1级2级3级……分类
     * 比如在表单提交时会用到
     * @returns {string}
     */
	this.getValuesStr = function() {
        var selects = cont.getElementsByTagName("select");
        var temp = "";
        for(var i=0; i<selects.length; i++){
            temp += ","+selects[i].value;
        }
        temp = temp.replace(/^,/gi, "");//去掉前边的逗号
        temp = temp.replace(/,0$/gi, "");//去掉后边的0及逗号
        return (temp === "0") ? "" : temp;
    };

    /**
	 * 得到最后一个SELECT的值，如果是空值就往前一个SELECT
     * @returns {number}
     */
    this.getValue = function() {
        var selects = cont.getElementsByTagName("select");
        for(var i=(selects.length-1); i>=0; i--){
            if(parseInt(selects[i].value) > 0){
            	return parseInt(selects[i].value);
			}
        }
        return 0;
    };

}


/**
 * 得到子分类
 * 得到指定分类ID的子分类
 * @param treeData 树形分类数据
 * @param cid
 */
NoLimitCategory.getChildren = function(cateTree, cid){
	for(var i=0; i<cateTree.length; i++){
		if(cateTree[i].id == cid){
			if(typeof(cateTree[i].children) != 'undefined'){
				return cateTree[i].children;
			}
		}
	}
	return null;
}


/**
 * 调用例子：
 *
 *
 *
 *
 var nlc = new NoLimitCategory(treeData, document.getElementById("cont_category"), "请选择");

 //如果要控制样式，回调onSelectCreate方法
 nlc.onSelectCreate = function(selectObj, n){
    $(selectObj).addClass("form-control2");
    if($(selectObj).parent().children('select').length>1){//如果不是第一个，给个间距
        $(selectObj).css('margin-left', '5px');
    }
};

 nlc.append();

 */